﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlaceHR.Processor.Processor.Vacation.Steps
{
    class SickVacationStepCalculator : StepCalculatorBase<VacationProcessorContext, IList<VacationBase>>
    {
        public override void Calculate(VacationProcessorContext context, IList<VacationBase> entity)
        {
            VacationBase currentVacationBase = context.HRDBContext.VacationBase.FirstOrDefault(p => p.EmployeeID == context.CurrentEmployee.EmployeeID
                                                && !p.IsDeleted
                                                && p.IsCurrent
                                                && p.VacationTypeCode != null
                                                && p.VacationTypeCode.Equals(VacationType.SickLeave, StringComparison.OrdinalIgnoreCase));

            VacationBase newVacationBase = entity.FirstOrDefault(p => p.VacationTypeCode.Equals(VacationType.SickLeave, StringComparison.OrdinalIgnoreCase));

            if (newVacationBase == null)
            {
                return;
            }

            VacationSettings currentSettings = context.CurrentVacationSettingsList.FirstOrDefault(p => p.VacationTypeCode != null
                            && p.VacationTypeCode.Equals(VacationType.SickLeave, StringComparison.OrdinalIgnoreCase));
            if (currentSettings == null)
            {
                return;
            }

            // 取以工作年限或入职日期为标准的最大工作时间
            int workMonth1 = context.CurrentEmployee.WorkingAge.HasValue ? context.CurrentEmployee.WorkingAge.Value * 12 : 0;
            int workMonth2 = context.CurrentEmployee.StartDate.HasValue ?
                ((context.CurrentEngine.Year - context.CurrentEmployee.StartDate.Value.Year) * 12 + (context.CurrentEngine.Month - context.CurrentEmployee.StartDate.Value.Month)) : 0;
            int workMonth = workMonth1 > workMonth2 ? workMonth1 : workMonth2;

            if (workMonth >= currentSettings.ActivateAfterWorkMonth)
            {
                if (currentVacationBase != null && currentVacationBase.VacationBaseId > 0)
                {
                    // 距离该种假期上次计算的时间（月）
                    int timespanMonth = (context.CurrentEngine.Year - currentVacationBase.Year) * 12 + (context.CurrentEngine.Month - currentVacationBase.Month);

                    if (timespanMonth >= currentSettings.IncrementTimespanMonth)
                    {
                        newVacationBase.InitialHours = currentVacationBase.FinalHours;
                        newVacationBase.MothIncrement = currentSettings.IncrementVacationHours;

                        decimal totalHours = newVacationBase.InitialHours.Value + newVacationBase.MothIncrement.Value;
                        newVacationBase.FinalHours = (currentSettings.MaxDays.HasValue && totalHours > currentSettings.MaxDays.Value * context.WorkingHoursPerDay)
                            ? currentSettings.MaxDays.Value * context.WorkingHoursPerDay : totalHours;
                    }
                    else
                    {
                        entity.Remove(newVacationBase);
                    }
                }
                else
                {
                    // 说明该种假期是第一次计算 （对于刚来的没满一个整月的，是否要按比例扣除？）
                    newVacationBase.InitialHours = currentSettings.InitialDays * 8;
                    newVacationBase.MothIncrement = currentSettings.IncrementVacationHours;
                    newVacationBase.FinalHours = newVacationBase.InitialHours + newVacationBase.MothIncrement;
                }
            }
            else
            {
                entity.Remove(newVacationBase);
            }
        }

    }
}
